/**
 * 
 */
package com.easidea.tim.action.util;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;

import com.easidea.tim.action.base.BaseAction;
import com.easidea.tim.entity.customer.Customer;
import com.easidea.tim.entity.finance.CustomerAccountListing;
import com.easidea.tim.entity.inventory.CheckInOut;
import com.easidea.tim.entity.inventory.CheckInOutItem;
import com.easidea.tim.service.customer.CustomerService;
import com.easidea.tim.service.finance.CustomerAccountListingService;
import com.easidea.tim.service.inventory.CheckInOutService;

/**
 * 打印用的Action
 * 
 * @author Harrison
 * 
 */
public class PrintAction extends BaseAction {
	private final static Logger log = Logger.getLogger(PrintAction.class);

	private CustomerAccountListingService customerAccountListingService;
	private CheckInOutService checkInOutService;
	private CustomerService customerService;

	private Long customerId;
	private String startDate;
	private String endDate;

	private List<CustomerAccountListing> cacList;
	private List<CheckInOutItem> checkInOutList;
	private Customer customer;
	private CheckInOutItem checkInOutItem;
	private Double customerCACLastAmount;
	private Double customerCACCurrentAmount;

	private Date today;
	private String purchaserName;
	private String type;
	private long checkInOutId;
	private CheckInOut checkInOut;
	/**
	 * 
	 */
	private static final long serialVersionUID = -2746520444078037060L;

	/**
	 * 打印客户的对账信息
	 * 
	 * @return
	 */
	public String printCustomerAccoutCheckingList() {
		today = new Date();
		if (customerId != null && StringUtils.isNotBlank(startDate)
				&& StringUtils.isNotBlank(endDate)) {
			try {
				Date stDate = parseDate(startDate, null);
				Date edDate = parseDate(endDate, null);

				Calendar calendar = GregorianCalendar.getInstance();
				calendar.setTime(edDate);
				calendar.add(Calendar.DATE, 1);
				edDate = calendar.getTime();

				customer = customerService.get(customerId);
				cacList = customerAccountListingService
						.getCustomerAccountListingArray(customerId, stDate,
								edDate);
				if (cacList != null && cacList.size() > 0) {
					customerCACLastAmount = cacList.get(0).getLastAmount(); // 上期记录
					customerCACCurrentAmount = cacList.get(cacList.size() - 1)
							.getTotalAmount();
				}
			} catch (Exception e) {
				log.error("parse Date error : " + e.getMessage(), e);
			}
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String printCheckInOutList() {
		today = new Date();
		checkInOut = checkInOutService.get(checkInOutId);
		checkInOutList = checkInOutService.getCheckInOutItemByID(checkInOutId);
		if (checkInOutList != null) {
			short otype = checkInOutList.get(0).getCheckInOut().getType();
			switch (otype) {
			case 1:
				type = "采购入库";
				break;

			case 2:
				type = "退货入库";
				break;
			case 3:
				type = "剪样新增入库";
				break;
			case 4:
				type = "剪样销售退回入库";
				break;
			case 5:
				type = "剪样领用退回入库";
				break;
			case -1:
				type = "销售出库";
				break;
			case -2:
				type = "采购退货出库";
				break;
			case -3:
				type = "剪样销售出库";
				break;
			case -4:
				type = "剪样零用出库";
				break;
			case -5:
				type = "产品转化剪样出库";
				break;
			}
			if (checkInOutList.get(0).getCheckInOut().getPurchaseOrder() != null) {
				purchaserName = checkInOutList.get(0).getCheckInOut()
						.getPurchaseOrder().getSupplier().getName();
			}
		}
		return SUCCESS;
	}

	/**
	 * 根据pattern 解析日期
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	private Date parseDate(String date, String[] pattern) throws ParseException {
		String[] defaultPattern = { "yyyy-MM-dd" };
		if (StringUtils.isNotBlank(date)) {
			if (pattern != null) {
				return DateUtils.parseDate(date, pattern);
			} else {
				return DateUtils.parseDate(date, defaultPattern);
			}
		}
		return null;
	}

	/**
	 * @return the customerAccountListingService
	 */
	public CustomerAccountListingService getCustomerAccountListingService() {
		return customerAccountListingService;
	}

	/**
	 * @param customerAccountListingService
	 *            the customerAccountListingService to set
	 */
	public void setCustomerAccountListingService(
			CustomerAccountListingService customerAccountListingService) {
		this.customerAccountListingService = customerAccountListingService;
	}

	/**
	 * @return the customerId
	 */
	public Long getCustomerId() {
		return customerId;
	}

	/**
	 * @param customerId
	 *            the customerId to set
	 */
	public void setCustomerId(Long customerId) {
		this.customerId = customerId;
	}

	/**
	 * @return the startDate
	 */
	public String getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate
	 *            the startDate to set
	 */
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the endDate
	 */
	public String getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate
	 *            the endDate to set
	 */
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the customerService
	 */
	public CustomerService getCustomerService() {
		return customerService;
	}

	/**
	 * @param customerService
	 *            the customerService to set
	 */
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	/**
	 * @return the cacList
	 */
	public List<CustomerAccountListing> getCacList() {
		return cacList;
	}

	/**
	 * @param cacList
	 *            the cacList to set
	 */
	public void setCacList(List<CustomerAccountListing> cacList) {
		this.cacList = cacList;
	}

	/**
	 * @return the customer
	 */
	public Customer getCustomer() {
		return customer;
	}

	/**
	 * @param customer
	 *            the customer to set
	 */
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	/**
	 * @return the customerCACLastAmount
	 */
	public Double getCustomerCACLastAmount() {
		return customerCACLastAmount;
	}

	/**
	 * @param customerCACLastAmount
	 *            the customerCACLastAmount to set
	 */
	public void setCustomerCACLastAmount(Double customerCACLastAmount) {
		this.customerCACLastAmount = customerCACLastAmount;
	}

	/**
	 * @return the today
	 */
	public Date getToday() {
		return today;
	}

	/**
	 * @param today
	 *            the today to set
	 */
	public void setToday(Date today) {
		this.today = today;
	}

	/**
	 * @return the customerCACCurrentAmount
	 */
	public Double getCustomerCACCurrentAmount() {
		return customerCACCurrentAmount;
	}

	/**
	 * @param customerCACCurrentAmount
	 *            the customerCACCurrentAmount to set
	 */
	public void setCustomerCACCurrentAmount(Double customerCACCurrentAmount) {
		this.customerCACCurrentAmount = customerCACCurrentAmount;
	}

	public List<CheckInOutItem> getCheckInOutList() {
		return checkInOutList;
	}

	public void setCheckInOutList(List<CheckInOutItem> checkInOutList) {
		this.checkInOutList = checkInOutList;
	}

	public CheckInOutService getCheckInOutService() {
		return checkInOutService;
	}

	public void setCheckInOutService(CheckInOutService checkInOutService) {
		this.checkInOutService = checkInOutService;
	}

	public long getCheckInOutId() {
		return checkInOutId;
	}

	public void setCheckInOutId(long checkInOutId) {
		this.checkInOutId = checkInOutId;
	}

	public String getPurchaserName() {
		return purchaserName;
	}

	public void setPurchaserName(String purchaserName) {
		this.purchaserName = purchaserName;
	}

	public CheckInOutItem getCheckInOutItem() {
		return checkInOutItem;
	}

	public void setCheckInOutItem(CheckInOutItem checkInOutItem) {
		this.checkInOutItem = checkInOutItem;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public CheckInOut getCheckInOut() {
		return checkInOut;
	}

	public void setCheckInOut(CheckInOut checkInOut) {
		this.checkInOut = checkInOut;
	}

	
}
